package flc.ast.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ImageUtils;

import flc.ast.R;
import stark.common.basic.utils.DensityUtil;
import stark.common.basic.utils.TimeUtil;

public abstract class BaseAvTrackView1 extends View {

    /**
     * 左边滑动栏的左边距离最左边的矩形区域 称之为 左边阴影区域
     */
    protected RectF mLeftShadowRectF;
    /**
     * 左边滑动栏所在的矩形区域
     */
    protected RectF mLeftSlideBarRectF;
    /**
     * 左边滑动栏能滑动到的最左边的位置，即mLeftSlideBarRectF.left的最小值
     */
    protected float mLeftSlideBarMinLeft;
    /**
     * 右边滑动栏的右边距离最右边的矩形区域 称之为 右边阴影区域
     */
    protected RectF mRightShadowRectF;
    /**
     * 右边滑动栏所在的矩形区域
     */
    protected RectF mRightSlideBarRectF;
    /**
     * 左边滑动栏能滑动到的最右边的位置，即mRightSlideBarRectF.right的最大值
     */
    protected float mRightSlideBarMaxRight;
    /**
     * 左右滑动栏之间的矩形区域
     */
    protected RectF mBetweenSlideBarRectF;
    /**
     * 是否触摸到左边滑动栏的标志
     */
    protected boolean isTouchLeftSlideBar = false;
    /**
     * 是否触摸到右边滑动栏的标志
     */
    protected boolean isTouchRightSlideBar = false;
    /**
     * 是否触摸到滑动栏之间区域的标志
     */
    protected boolean isTouchBetweenSlideBar = false;
    protected float mPreX, mPreY;
    protected int mTouchSlop;
    /**
     * 每毫秒所占的间距宽度
     */
    protected float mWidthPerMillis = DensityUtil.dip2px(1) / 1000f;
    protected Bitmap mLeftSlideBarBmp;
    protected Bitmap mRightSlideBarBmp;
    private int mShadowOffset;
    /**
     * 是否显示开始、结束时间的标志
     */
    protected boolean mShowTime = false;
    protected TimePos mTimePos = TimePos.TOP;
    protected int mTimePadding = DensityUtil.dip2px(5);
    /**
     * 音频或视频时长 单位：毫秒
     */
    protected long mDuration;
    /**
     * 时长的总宽度
     */
    protected int mDurationWidth;
    /**
     * 裁剪的开始时间、结束时间
     */
    protected long mStartTime, mEndTime;
    /**
     * 记录触摸按下时的开始时间及结束时间
     */
    private long mDownStartTime, mDownEndTime;
    /**
     * 当前媒体播放到时间
     */
    private long mPlayTime;
    protected String mStrStartTime = TimeUtil.getMmss(mStartTime);
    protected String mStrEndTime = TimeUtil.getMmss(mEndTime);
    protected float mStartTimeX, mStartTimeY;
    protected float mEndTimeX, mEndTimeY;

    protected Paint mCommonPaint;
    protected Paint mTimePaint;
    protected Paint mPlayLinePaint;
    protected float mTimeTextHeight;
    protected float mShadowRadius = 10f;
    protected int mTrackBgColor = Color.parseColor("#000000");
    protected int mShadowColor = Color.parseColor("#50666666");
    protected int mMiddleShadowColor = Color.parseColor("#00000000");

    public BaseAvTrackView1(Context context) {
        this(context, null);
    }

    public BaseAvTrackView1(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BaseAvTrackView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(attrs);
    }

    protected Paint createPaint() {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        return paint;
    }

    protected void initAttrs(AttributeSet attrs) {
        int timeTextSize = DensityUtil.dip2px(10);
        int timeTextColor = Color.parseColor("#000000");
        int playLineColor = Color.parseColor("#FFFFFF");
        int playLineWidth = DensityUtil.dip2px(2);
        if (attrs != null) {
            TypedArray array = getContext().obtainStyledAttributes(attrs, R.styleable.BaseAvTrackView);

            mTrackBgColor = array.getColor(R.styleable.BaseAvTrackView_av_track_bg_color, mTrackBgColor);
            mShadowColor = array.getColor(R.styleable.BaseAvTrackView_av_shadow_color, mShadowColor);
            mMiddleShadowColor = array.getColor(R.styleable.BaseAvTrackView_av_middle_shadow_color, mMiddleShadowColor);
            mShadowRadius = array.getDimension(R.styleable.BaseAvTrackView_av_shadow_radius, mShadowRadius);
            Drawable drawable = array.getDrawable(R.styleable.BaseAvTrackView_av_left_slide_bar);
            if (drawable != null) {
                mLeftSlideBarBmp = ImageUtils.drawable2Bitmap(drawable);
            }
            if (mLeftSlideBarBmp == null) {
                mLeftSlideBarBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ayidongtiao);
            }
            //AssertUtil.assertForNull(mLeftSlideBarBmp, "You have to set av_left_slide_bar attr to provide a bitmap to mLeftSlideBarBmp");
            drawable = array.getDrawable(R.styleable.BaseAvTrackView_av_right_slide_bar);
            if (drawable != null) {
                mRightSlideBarBmp = ImageUtils.drawable2Bitmap(drawable);
            }
            if (mRightSlideBarBmp == null) {
                mRightSlideBarBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ayouyidongt);
                ;
            }
            //AssertUtil.assertForNull(mRightSlideBarBmp, "You have to set av_right_slide_bar attr to provide a bitmap to mRightSlideBarBmp");

            //time text
            mShowTime = array.getBoolean(R.styleable.BaseAvTrackView_av_time_visible, false);
            timeTextSize = array.getDimensionPixelSize(R.styleable.BaseAvTrackView_av_time_text_size, timeTextSize);
            timeTextColor = array.getColor(R.styleable.BaseAvTrackView_av_time_text_color, timeTextColor);
            int pos = array.getInt(R.styleable.BaseAvTrackView_av_time_pos, com.banshengyanyu.bottomtrackviewlib.BaseAvTrackView.TimePos.TOP.ordinal());
            mTimePos = TimePos.getTimePos(pos);
            mTimePadding = array.getDimensionPixelSize(R.styleable.BaseAvTrackView_av_time_text_padding, mTimePadding);
            mShadowOffset = array.getDimensionPixelSize(R.styleable.BaseAvTrackView_av_shadow_offset, 0);
            playLineColor = array.getColor(R.styleable.BaseAvTrackView_av_play_line_color, playLineColor);
            playLineWidth = array.getDimensionPixelSize(R.styleable.BaseAvTrackView_av_play_line_width, playLineWidth);

            array.recycle();
        }
        mCommonPaint = createPaint();

        mTimePaint = createPaint();
        mTimePaint.setTextSize(timeTextSize);
        mTimePaint.setColor(timeTextColor);
        updateTimePaint();

        mPlayLinePaint = createPaint();
        mPlayLinePaint.setColor(playLineColor);
        mPlayLinePaint.setStrokeWidth(playLineWidth);

        ViewConfiguration viewConfiguration = ViewConfiguration.get(getContext());
        mTouchSlop = viewConfiguration.getScaledTouchSlop();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measuredWidth = getMeasuredWidth();
        setMeasuredDimension(measuredWidth, getCalHeight());

        //left slide bar rect
        mLeftSlideBarMinLeft = getPaddingLeft();
        mLeftSlideBarRectF = new RectF(0, 0, mLeftSlideBarBmp.getWidth(), mLeftSlideBarBmp.getHeight());
        if (mShowTime && mTimePos == TimePos.TOP) {
            mLeftSlideBarRectF.offset(getPaddingLeft(), getPaddingTop() + mTimeTextHeight + mTimePadding);
        } else {
            mLeftSlideBarRectF.offset(getPaddingLeft(), getPaddingTop());
        }
        mLeftShadowRectF = new RectF(mLeftSlideBarRectF);
        mLeftShadowRectF.top += mShadowOffset;
        mLeftShadowRectF.bottom -= mShadowOffset;

        //start time
        updateStartTimePosition();

        //right slide bar rect
        mRightSlideBarMaxRight = measuredWidth - getPaddingRight();
        mRightSlideBarRectF = new RectF(mLeftSlideBarRectF);
        mRightSlideBarRectF.right = mRightSlideBarMaxRight;
        mRightSlideBarRectF.left = mRightSlideBarRectF.right - mLeftSlideBarRectF.width();
        mRightShadowRectF = new RectF(mRightSlideBarRectF);
        mRightShadowRectF.top = mLeftShadowRectF.top;
        mRightShadowRectF.bottom = mLeftShadowRectF.bottom;

        //end time
        updateEndTimePosition();

        //between slide bar rect
        mBetweenSlideBarRectF = new RectF();
        mBetweenSlideBarRectF.left = mLeftSlideBarRectF.right;
        mBetweenSlideBarRectF.right = mRightSlideBarRectF.left;
        mBetweenSlideBarRectF.top = mLeftShadowRectF.top;
        mBetweenSlideBarRectF.bottom = mLeftShadowRectF.bottom;

        //duration width
        mDurationWidth = (int) (mRightSlideBarMaxRight - mLeftSlideBarMinLeft - mLeftSlideBarBmp.getWidth() * 2);
        setDuration(mDuration);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制轨道背景层
        mCommonPaint.setColor(mTrackBgColor);
        RectF rectF = new RectF(mLeftShadowRectF);
        rectF.left = mLeftSlideBarMinLeft;
        rectF.right = mRightSlideBarMaxRight;
        canvas.drawRoundRect(rectF, mShadowRadius, mShadowRadius, mCommonPaint);

        //绘制视频帧或音频的波形图层
        drawTrackContent(canvas);

        //绘制左右边的阴影层
        mCommonPaint.setColor(mShadowColor);
        canvas.drawRoundRect(mLeftShadowRectF, mShadowRadius, mShadowRadius, mCommonPaint);
        canvas.drawRoundRect(mRightShadowRectF, mShadowRadius, mShadowRadius, mCommonPaint);

        //绘制左右边滑动栏之间的阴影层
        mCommonPaint.setColor(mMiddleShadowColor);
        canvas.drawRoundRect(mBetweenSlideBarRectF, 0, 0, mCommonPaint);

        //绘制左右滑动栏
        canvas.drawBitmap(mLeftSlideBarBmp, null, mLeftSlideBarRectF, null);
        canvas.drawBitmap(mRightSlideBarBmp, null, mRightSlideBarRectF, null);

        //绘制当前播放到的位置的竖线
        float startX = mLeftSlideBarMinLeft + mLeftSlideBarRectF.width() + mPlayTime * mWidthPerMillis;
        float stopX = startX;
        float startY = mLeftShadowRectF.top;
        float stopY = mLeftShadowRectF.bottom;
        canvas.drawLine(startX, startY, stopX, stopY, mPlayLinePaint);

        //绘制开始时间、结束时间
        if (!mShowTime) {
            return;
        }
        canvas.drawText(mStrStartTime, mStartTimeX, mStartTimeY, mTimePaint);
        canvas.drawText(mStrEndTime, mEndTimeX, mEndTimeY, mTimePaint);
    }

    /**
     * 绘制轨道内容 音频绘制音频波形 视频则绘制帧图片 子类自己去实现
     *
     * @param canvas
     */
    protected abstract void drawTrackContent(Canvas canvas);

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mDownStartTime = mStartTime;
                mDownEndTime = mEndTime;
                mPreX = event.getX();
                mPreY = event.getY();
                if (mLeftSlideBarRectF.contains(mPreX, mPreY)) {
                    isTouchLeftSlideBar = true;
                    return true;
                }
                if (mRightSlideBarRectF.contains(mPreX, mPreY)) {
                    isTouchRightSlideBar = true;
                    return true;
                }
                if (mBetweenSlideBarRectF.contains(mPreX, mPreY)) {
                    isTouchBetweenSlideBar = true;
                    return true;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                float curX = event.getX();
                float deltaX = curX - mPreX;
                mPreX = curX;
                if (isTouchLeftSlideBar) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    moveLeftSlideBar(deltaX);
                    return true;
                } else if (isTouchRightSlideBar) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    moveRightSlideBar(deltaX);
                    return true;
                } else if (isTouchBetweenSlideBar) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    moveBetweenSlideBar(deltaX);
                    return true;
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if (mListener != null) {
                    if (isTouchLeftSlideBar) {
                        if (mStartTime != mDownStartTime) {
                            mListener.onStartTimeChanged(mStartTime);
                        }
                    } else if (isTouchRightSlideBar) {
                        if (mEndTime != mDownEndTime) {
                            mListener.onEndTimeChanged(mEndTime);
                        }
                    } else if (isTouchBetweenSlideBar) {
                        if (mStartTime != mDownStartTime || mEndTime != mDownEndTime) {
                            mListener.onStartEndTimeChanged(mStartTime, mEndTime);
                        }
                    }
                }
                isTouchLeftSlideBar = false;
                isTouchRightSlideBar = false;
                isTouchBetweenSlideBar = false;
                break;
        }
        return super.onTouchEvent(event);
    }

    protected int getCalHeight() {
        int calHeight = getPaddingTop() + getPaddingBottom();
        //time text
        if (mShowTime && (mTimePos == TimePos.TOP || mTimePos == TimePos.BOTTOM)) {
            calHeight += mTimeTextHeight;
            calHeight += mTimePadding;
        }

        //slide bar height
        calHeight += mLeftSlideBarBmp.getHeight();
        return calHeight;
    }

    /**
     * 设置时长
     *
     * @param duration 单位：毫秒
     */
    public void setDuration(long duration) {
        if (duration <= 0 || mDuration == duration) {
            return;
        }
        this.mDuration = duration;
        mEndTime = mDuration;
        mStrEndTime = TimeUtil.getMmss(mEndTime);
        if (mDurationWidth > 0) {
            mWidthPerMillis = mDurationWidth * 1f / mDuration;
            updateEndTimePosition();
        }
        invalidate();
    }

    /**
     * 滑动左边滑动栏的处理
     *
     * @param deltaX 滑动的距离
     */
    protected void moveLeftSlideBar(float deltaX) {
        if (deltaX == 0) {
            return;
        }
        float canDeltaX = deltaX;
        if (deltaX > 0) {
            //左边滑动栏往右边滑动
            if (mRightSlideBarRectF.left - mLeftSlideBarRectF.right < deltaX) {
                canDeltaX = mLeftSlideBarRectF.right - mRightSlideBarRectF.left;
            }
            mLeftSlideBarRectF.offset(canDeltaX, 0);
            mBetweenSlideBarRectF.left += canDeltaX;
            if (mBetweenSlideBarRectF.left > mBetweenSlideBarRectF.right) {
                mBetweenSlideBarRectF.left = mBetweenSlideBarRectF.right;
            }
        } else {
            //左边滑动栏往左边滑动
            if (mLeftSlideBarMinLeft - mLeftSlideBarRectF.left > deltaX) {
                canDeltaX = mLeftSlideBarMinLeft - mLeftSlideBarRectF.left;
            }
            mLeftSlideBarRectF.offset(canDeltaX, 0);
            mBetweenSlideBarRectF.left += canDeltaX;
            if (mBetweenSlideBarRectF.left < mLeftSlideBarRectF.right) {
                mBetweenSlideBarRectF.left = mLeftSlideBarRectF.right;
            }
        }
        if (canDeltaX != 0) {
            mLeftShadowRectF.right = mLeftSlideBarRectF.right;
            mStartTime = (long) ((mLeftSlideBarRectF.left - mLeftSlideBarMinLeft) / mWidthPerMillis);
            mStrStartTime = TimeUtil.getMmss(mStartTime);
            updateStartTimePosition();
            invalidate();
        }
    }

    /**
     * 滑动右边滑动栏的处理
     *
     * @param deltaX 滑动的距离
     */
    protected void moveRightSlideBar(float deltaX) {
        if (deltaX == 0) {
            return;
        }
        float canDeltaX = deltaX;
        if (deltaX > 0) {
            //右边滑动栏往右边滑动
            if (mRightSlideBarMaxRight - mRightSlideBarRectF.right < deltaX) {
                canDeltaX = mRightSlideBarMaxRight - mRightSlideBarRectF.right;
            }
            mRightSlideBarRectF.offset(canDeltaX, 0);
            mBetweenSlideBarRectF.right += canDeltaX;
            if (mBetweenSlideBarRectF.right > mRightSlideBarRectF.left) {
                mBetweenSlideBarRectF.right = mRightSlideBarRectF.left;
            }
        } else {
            //右边滑动栏往左边滑动
            if (mLeftSlideBarRectF.right - mRightSlideBarRectF.left > deltaX) {
                canDeltaX = mLeftSlideBarRectF.right - mRightSlideBarRectF.left;
            }
            mRightSlideBarRectF.offset(canDeltaX, 0);
            mBetweenSlideBarRectF.right += canDeltaX;
            if (mBetweenSlideBarRectF.right < mBetweenSlideBarRectF.left) {
                mBetweenSlideBarRectF.right = mBetweenSlideBarRectF.left;
            }
        }
        if (canDeltaX != 0) {
            mRightShadowRectF.left = mRightSlideBarRectF.left;
            mEndTime = (long) ((mRightSlideBarRectF.left - mLeftSlideBarMinLeft - mLeftSlideBarRectF.width()) / mWidthPerMillis);
            mStrEndTime = TimeUtil.getMmss(mEndTime);
            updateEndTimePosition();
            invalidate();
        }
    }

    /**
     * 触摸左右滑动栏之间的区域而引起左右滑动栏同时滑动的处理
     *
     * @param deltaX 滑动的距离
     */
    protected void moveBetweenSlideBar(float deltaX) {
        if (deltaX == 0) {
            return;
        }
        float canDeltaX = deltaX;
        if (deltaX > 0) {
            //都要向右边滑动
            if (mRightSlideBarMaxRight - mRightSlideBarRectF.right < deltaX) {
                canDeltaX = mRightSlideBarMaxRight - mRightSlideBarRectF.right;
            }
        } else {
            //都要向左边滑动
            if (mLeftSlideBarMinLeft - mLeftSlideBarRectF.left > deltaX) {
                canDeltaX = mLeftSlideBarMinLeft - mLeftSlideBarRectF.left;
            }
        }
        if (canDeltaX != 0) {
            mLeftSlideBarRectF.offset(canDeltaX, 0);
            mBetweenSlideBarRectF.offset(canDeltaX, 0);
            mRightSlideBarRectF.offset(canDeltaX, 0);

            mLeftShadowRectF.right = mLeftSlideBarRectF.right;
            mRightShadowRectF.left = mRightSlideBarRectF.left;

            mStartTime = (long) ((mLeftSlideBarRectF.left - mLeftSlideBarMinLeft) / mWidthPerMillis);
            mEndTime = (long) ((mRightSlideBarRectF.left - mLeftSlideBarMinLeft - mLeftSlideBarRectF.width()) / mWidthPerMillis);
            mStrStartTime = TimeUtil.getMmss(mStartTime);
            mStrEndTime = TimeUtil.getMmss(mEndTime);
            updateStartTimePosition();
            updateEndTimePosition();
            invalidate();
        }
    }

    private void updateTimePaint() {
        if (mTimePaint == null) {
            return;
        }
        if (mTimePos == TimePos.CENTER) {
            mTimePaint.setTextAlign(Paint.Align.LEFT);
        } else {
            mTimePaint.setTextAlign(Paint.Align.CENTER);
        }
        Paint.FontMetrics fontMetrics = mTimePaint.getFontMetrics();
        mTimeTextHeight = Math.abs(fontMetrics.bottom) + Math.abs(fontMetrics.top);
    }

    protected void updateStartTimePosition() {
        if (mTimePos == TimePos.TOP || mTimePos == TimePos.BOTTOM) {
            if (mTimePos == TimePos.TOP) {
                mStartTimeY = getPaddingTop() + Math.abs(mTimePaint.getFontMetrics().top);
            } else {
                mStartTimeY = mLeftSlideBarRectF.bottom + mTimePadding + Math.abs(mTimePaint.getFontMetrics().top);
            }
            mStartTimeX = mLeftSlideBarRectF.left + mLeftSlideBarRectF.width() / 2;
            float startTimeWidth = mTimePaint.measureText(mStrStartTime);
            //左边边界检测
            if (mStartTimeX - startTimeWidth / 2 < getPaddingLeft()) {
                //越界则将x往右边移动对应位置
                mStartTimeX += getPaddingLeft() - (mStartTimeX - startTimeWidth / 2);
            }
        } else {
            mStartTimeX = mLeftSlideBarRectF.right + mTimePadding;
            mStartTimeY = mLeftSlideBarRectF.top + mLeftSlideBarRectF.height() / 2;
        }
    }

    protected void updateEndTimePosition() {
        float endTimeWidth = mTimePaint.measureText(mStrEndTime);
        if (mTimePos == TimePos.TOP || mTimePos == TimePos.BOTTOM) {
            if (mTimePos == TimePos.TOP) {
                mEndTimeY = getPaddingTop() + Math.abs(mTimePaint.getFontMetrics().top);
            } else {
                mEndTimeY = mRightSlideBarRectF.bottom + mTimePadding + Math.abs(mTimePaint.getFontMetrics().top);
            }
            mEndTimeX = mRightSlideBarRectF.left + mRightSlideBarRectF.width() / 2;
            //右边边界检测
            if (mEndTimeX + endTimeWidth / 2 > (getMeasuredWidth() - getPaddingRight())) {
                //越界则将x坐标往左边移动对应位置
                mEndTimeX -= (mEndTimeX + endTimeWidth / 2) - (getMeasuredWidth() - getPaddingRight());
            }
        } else {
            mEndTimeX = mRightSlideBarRectF.left - mTimePadding - endTimeWidth;
            mEndTimeY = mRightSlideBarRectF.top + mRightSlideBarRectF.height() / 2;
        }
    }

    /**
     * 裁剪的开始时间 单位：毫秒
     *
     * @return
     */
    public long getStartTime() {
        return mStartTime;
    }

    /**
     * 裁剪的结束时间 单位：毫秒
     *
     * @return
     */
    public long getEndTime() {
        return mEndTime;
    }

    /**
     * 获取该控件正在处理的媒体文件的时长
     *
     * @return 单位：毫秒
     */
    public long getDuration() {
        return mDuration;
    }

    public void setShadowColor(int shadowColor) {
        this.mShadowColor = shadowColor;
        invalidate();
    }

    public void setMiddleShadowColor(int middleShadowColor) {
        this.mMiddleShadowColor = middleShadowColor;
        invalidate();
    }

    public void setTrackBgColor(int trackBgColor) {
        this.mTrackBgColor = trackBgColor;
        invalidate();
    }

    public void setTimeTextSize(float textSize) {
        if (mTimePaint != null) {
            mTimePaint.setTextSize(textSize);
            updateTimePaint();
            requestLayout();
        }
    }

    public void setTimeTextColor(@ColorInt int textColor) {
        if (mTimePaint != null) {
            mTimePaint.setColor(textColor);
            invalidate();
        }
    }

    public void setTimePos(TimePos timePos) {
        if (mTimePos == timePos) return;
        this.mTimePos = timePos;
        updateTimePaint();
        requestLayout();
    }

    /**
     * 设置播放到的时间
     *
     * @param playTime 单位：毫秒
     */
    public void setPlayTime(long playTime) {
        if (mPlayTime == playTime) return;
        mPlayTime = playTime;
        invalidate();
    }

    protected IListener mListener;

    public void setListener(IListener listener) {
        this.mListener = listener;
    }

    /**
     * 监听器
     */
    public interface IListener {

        /**
         * 左边滑动栏滑动时的回调方法
         *
         * @param startTime 开始时间，单位：毫秒
         */
        void onStartTimeChanged(long startTime);

        /**
         * 右边滑动栏滑动时的回调方法
         *
         * @param endTime 结束时间，单位：毫秒
         */
        void onEndTimeChanged(long endTime);

        /**
         * 触摸左右滑动栏之间的区域滑动时的回调方法
         *
         * @param startTime 开始时间，单位：毫秒
         * @param endTime   结束时间，单位：毫秒
         */
        void onStartEndTimeChanged(long startTime, long endTime);

    }

    /**
     * 时间显示位置枚举常量类
     */
    public enum TimePos {
        TOP,
        CENTER,
        BOTTOM,
        ;

        public static TimePos getTimePos(int pos) {
            for (TimePos timePos : TimePos.values()) {
                if (timePos.ordinal() == pos) {
                    return timePos;
                }
            }
            return null;
        }
    }
}

